package com.example.administrator.szb.activity.teseservice;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.SeriveList;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.CheckInfoUtils;
import com.example.administrator.szb.util.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.api.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeseActivity extends MVPBaseActivity {
    private SmartRefreshLayout freshlayout;
    private View error_net_ll,noda_ll;
    private ListView listView;
    private LinearLayout ll_error;
    private ImageView iv_error;
    private TextView tv_net_error,bar_title,bar_right;
    private TextView tv_tips;
    private Button btn_error;
    private LinearLayout no_data_ll;
    private ImageView iv_no_data;
    private TextView tv_no_data;
    private TextView tv_tips2;
    private Button no_data_btn;
    private List list = new ArrayList();
    private boolean isFirstEnter = true;
    int current_page = 1 ;
    int per_page = 10;
    private Teseadapter2 adapter;
    private int types=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tese);
    }
    private boolean showRight;
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        error_net_ll = findViewById(R.id.noda_ll);
        noda_ll = findViewById(R.id.noda_ll);
        bar_title = findViewById(R.id.bar_title);

        bar_right = findViewById(R.id.bar_right);
        bar_title.setText("特色服务");
        bar_right.setText("发布服务");
        showRight = getIntent().getBooleanExtra("showRight",false);
        if (showRight){
            bar_right.setVisibility(View.VISIBLE);
        }
        freshlayout = (SmartRefreshLayout) findViewById(R.id.freshlayout);
        listView = (ListView) findViewById(R.id.listView);
        listView.setBackgroundColor(Color.parseColor("#F3F3F3"));
        ll_error = (LinearLayout) findViewById(R.id.ll_error);
        iv_error = (ImageView) findViewById(R.id.iv_error);
        tv_net_error = (TextView) findViewById(R.id.tv_net_error);
        tv_tips = (TextView) findViewById(R.id.tv_tips);
        btn_error = (Button) findViewById(R.id.btn_error);
        no_data_ll = (LinearLayout) findViewById(R.id.no_data_ll);
        iv_no_data = (ImageView) findViewById(R.id.iv_no_data);
        tv_no_data = (TextView) findViewById(R.id.tv_no_data);
        tv_no_data.setText("您还没有发布过特色服务\n快去发布您的特色服务\n让上市公司一眼看到你的优势吧");
        tv_tips2 = (TextView) findViewById(R.id.tv_tips2);
        no_data_btn = (Button) findViewById(R.id.no_data_btn);
        types = getIntent().getIntExtra("type",0);
        if (showRight){
            types=1;
        }
    }
    @Override
    protected void initEvent() {
        bar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckInfoUtils.check(activity, iosDiaolog, bar_right, new CheckInfoUtils.OnSuccessLietener() {
                    @Override
                    public void onSuccess() {
                        Intent intent = new Intent(context,TeseFabuActivity.class);
                        startActivity(intent);
                    }
                });

            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, TeseDeatils.class);
                intent.putExtra("id",((SeriveList)list.get(position)).getId());
                startActivity(intent);
            }
        });
        if (isFirstEnter) {
            isFirstEnter = false;
            freshlayout.autoRefresh();
        }
        freshlayout.setEnableLoadMore(true);//是否启用上拉加载功能
        freshlayout.setEnableLoadMoreWhenContentNotFull(true);//是否在列表不满一页时候开启上拉加载功能


        freshlayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                int page = current_page+1;
                getService(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                current_page = 1;
                getService(current_page);
            }
        });
    }

    @Override
    protected void initData() {

        adapter = new Teseadapter2(context,list);
        adapter.setShowStatus(showRight);
        listView.setAdapter(adapter);
    }
    public void getService(final int page){
        freshlayout.setVisibility(View.VISIBLE);
        noda_ll.setVisibility(View.GONE);
        error_net_ll.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
        String url = URLAddress.SPECIAL_GETALL;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("page",page);
        map.put("page_count",per_page);
        if (types==1){
            map.put("uid", SPUtils.getUserId());
        }
        HttpUtils.post(activity,url,map ,new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                List l = JSON.parseArray(str, SeriveList.class);
                if (page==1){
                    list.clear();

                    //刷新
                    freshlayout.finishRefresh();
                    freshlayout.setEnableLoadMore(true);
                    current_page=1;
                }else {
                    freshlayout.finishLoadmore();
                    current_page++;
                    if (l.size()<per_page){

                        //没有更多了
                        freshlayout.setEnableLoadMore(false);

                    }
                }

                list.addAll(l);
                if (list.size()==0){
                    listView.setVisibility(View.GONE);
                    noda_ll.setVisibility(View.VISIBLE);
                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int requestCode, String str) {
                freshlayout.setVisibility(View.GONE);

                error_net_ll.setVisibility(View.VISIBLE);
            }
        });
    }
}
